﻿using System;
using fastCSharp.code.cSharp;

namespace diantou.web.include
{
    /// <summary>
    /// 搜索视图
    /// </summary>
    /// <typeparam name="viewType"></typeparam>
    abstract class search<viewType> : view<viewType>
        where viewType : search<viewType>
    {
        /// <summary>
        /// 搜索结果
        /// </summary>
        [fastCSharp.emit.webView.clearMember]
        protected diantou.dataAccess.searchItem[] results;
        /// <summary>
        /// 
        /// </summary>
        protected webView.hashUrl searchPath;
        /// <summary>
        /// 关键字
        /// </summary>
        [fastCSharp.code.cSharp.webView.outputAjax]
        protected string key;
        /// <summary>
        /// 分页号
        /// </summary>
        protected int page;
        /// <summary>
        /// 是否匹配话题名称
        /// </summary>
        protected bool isTopic;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="type"></param>
        /// <param name="page"></param>
        /// <param name="ignoreIds"></param>
        /// <param name="noBlack"></param>
        /// <param name="isTopic"></param>
        /// <param name="onlyPath"></param>
        /// <returns></returns>
        protected bool loadView(string key, dataAccess.search.type type, int page, int[] ignoreIds, bool noBlack, bool isTopic, bool onlyPath)
        {
            this.key = search.FormatKey(key);
            this.page = page;
            this.isTopic = isTopic;
            searchPath = diantou.dataAccess.searchItem.GetSearchPath(key, type);
            if (!onlyPath)
            {
                results = diantou.dataAccess.tcpCall.searchItem.get(new diantou.dataAccess.searchInfo { Keyword = this.key, Type = type, Page = page, PageSize = fastCSharp.config.pub.Default.PageSize, IgnoreIds = ignoreIds }, currentUserId, noBlack, out this.page, ref this.isTopic);
            }
            return true;
        }
    }
    /// <summary>
    /// 通用搜索
    /// </summary>
    [fastCSharp.code.cSharp.webView(IsPage = false, IsPool = true, QueryName = null)]
    partial class search : search<search>
    {
        /// <summary>
        /// 最大搜索关键字长度
        /// </summary>
        private const int maxKeyLength = 30;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        internal static string FormatKey(string key)
        {
            return key != null ? (key.Length > maxKeyLength ? key.Substring(0, maxKeyLength) : key) : null;
        }
    }
}
